<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/page/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/page/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/page/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/page/images/logo.svg" color="#222">

<link rel="stylesheet" href="/page/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"assd12138.gitee.io","root":"/page/","images":"/page/images","scheme":"Gemini","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/page/search.json","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/page/js/config.js"></script>
<meta name="description" content="BFC又称块级格式化上下文，可以用来创建一个内部元素不会影响外部元素的封闭区域">
<meta property="og:type" content="article">
<meta property="og:title" content="BFC笔记">
<meta property="og:url" content="https://assd12138.gitee.io/page/2020/08/28/BFC%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="爱爱爱呵风他">
<meta property="og:description" content="BFC又称块级格式化上下文，可以用来创建一个内部元素不会影响外部元素的封闭区域">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-28T01:14:01.000Z">
<meta property="article:modified_time" content="2022-05-09T03:18:13.061Z">
<meta property="article:author" content="爱爱爱呵风他">
<meta property="article:tag" content="CSS">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://assd12138.gitee.io/page/2020/08/28/BFC%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://assd12138.gitee.io/page/2020/08/28/BFC%E7%AC%94%E8%AE%B0/","path":"2020/08/28/BFC笔记/","title":"BFC笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>BFC笔记 | 爱爱爱呵风他</title>
  




  <noscript>
    <link rel="stylesheet" href="/page/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/page/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">爱爱爱呵风他</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/page/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/page/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-archives"><a href="/page/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#BFC%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">1.</span> <span class="nav-text">BFC解决的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A7%A6%E5%8F%91%E6%96%B9%E5%BC%8F"><span class="nav-number">2.</span> <span class="nav-text">触发方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#BFC%E4%BC%9A%E8%A7%A6%E5%8F%91%E7%9A%84%E8%BF%90%E7%AE%97%E8%A7%84%E5%88%99"><span class="nav-number">3.</span> <span class="nav-text">BFC会触发的运算规则</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="爱爱爱呵风他"
      src="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
  <p class="site-author-name" itemprop="name">爱爱爱呵风他</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/page/archives/">
          <span class="site-state-item-count">24</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/page/tags/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://assd12138.gitee.io/page/2020/08/28/BFC%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
      <meta itemprop="name" content="爱爱爱呵风他">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱爱爱呵风他">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          BFC笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-08-28 09:14:01" itemprop="dateCreated datePublished" datetime="2020-08-28T09:14:01+08:00">2020-08-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-09 11:18:13" itemprop="dateModified" datetime="2022-05-09T11:18:13+08:00">2022-05-09</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>BFC又称块级格式化上下文，可以用来创建一个内部元素不会影响外部元素的封闭区域</p>
<span id="more"></span>

<h2 id="BFC解决的问题"><a href="#BFC解决的问题" class="headerlink" title="BFC解决的问题"></a>BFC解决的问题</h2><p>当没有进行BFC处理的时候，父元素内部子元素的变化可能会影响页面其他部分的布局，见下面的示例</p>
<hr>

<div style="overflow: hidden;">
  <label for="overflow">点击切换父元素overflow:hidden<input id="overflow" type="checkbox" /></label>
  <div class="warp">
    &nbsp;
    <div class="son" style=" background-color: aqua;"></div>
    <div class="son" style="background-color: red;"></div>
  </div>
  <div>anonther DIV</div>
</div>
<script>
  let check = document.querySelector('#overflow')
  check.checked = false
  let warp = document.querySelector('.warp')
  check.addEventListener('click',()=>{
    warp.style.overflow = warp.style.overflow === 'hidden' ? 'visible' : 'hidden'
  })
</script>
<style>
  .warp {
    width: 400px;
    background-color: chartreuse;
  }

  .son {
    margin: 10px;
    float: left;
    background-color: aqua;
    width: 33%;
    height: 200px;
  }
</style>

<hr>
<p>如果没有设置父元素<strong>overflow</strong>的值为除了<strong>visiable</strong>的其他值，不触发BFC，那么全部浮动的子元素会使得父元素高度塌陷，后面的布局就会被缩到上面去，也就是子元素的浮动使得其他后续布局被打乱。BFC以后，子元素再怎么设置，外部也不会受到布局被打乱的影响</p>
<hr>

<div style="overflow: hidden;">
    <label for="isBFC-1">右栏是否BFC<input type="checkbox" id="isBFC-1"></label>
    <button id="add-1">增加右栏文字</button>
    <div class="father-1">
      <div class="son-1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium nulla aliquam, laboriosam
        mollitia magnam assumenda dolorum ducimus harum excepturi illo culpa, voluptates expedita quisquam omnis
        exercitationem, iste voluptatum inventore veritatis.</div>
      <div class="son-2"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Asperiores, commodi quia! Earum
        repellat ipsam accusantium facere alias sequi, pariatur itaque modi eum atque nemo id consequatur perspiciatis
        corporis voluptatum assumenda.</div>
    </div>
  </div>
  <script>
  let check_1 = document.querySelector('#isBFC-1')
  let add_1 = document.querySelector('#add-1')
  let son_2 = document.querySelector('.son-2')
  check_1.checked = false
  add_1.addEventListener('click', e => {
    son_2.textContent += 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Asperiores, commodi quia! Earumrepellat ipsam accusantium facere alias sequi, pariatur itaque modi eum atque nemo id consequatur perspiciatiscorporis voluptatum assumenda'
  })
  check_1.addEventListener('click',e=>{
    son_2.style.overflow = son_2.style.overflow === 'hidden' ? 'visible' : 'hidden'
  })
</script>
<style>
  .father-1 {
    border: 1px solid red;
  }

  .son-1 {
    float: left;
    width: 200px;
    background-color: bisque;
  }

  .son-2 {
    background-color: cornflowerblue;
  }
</style>

<hr>
<p>如果利用浮动实现两栏布局，右侧的文字不触发BFC，那么超出左侧高度后触发环绕效果，失去两栏布局。为右侧栏触发一下BFC，就形成了一个独立的容器，避免了环绕。</p>
<h2 id="触发方式"><a href="#触发方式" class="headerlink" title="触发方式"></a>触发方式</h2><p>在容器元素上满足以下一项(有时浮动等可能会影响整体布局，因此要选择合适的方法触发BFC)时触发</p>
<ol>
<li>根元素</li>
<li>float属性不为none</li>
<li>position为absolute或fixed</li>
<li>display为inline-block, table-cell, table-caption, flex, inline-flex</li>
<li>overflow不为visible</li>
</ol>
<h2 id="BFC会触发的运算规则"><a href="#BFC会触发的运算规则" class="headerlink" title="BFC会触发的运算规则"></a>BFC会触发的运算规则</h2><ol>
<li>内部的Box会在垂直方向，一个接一个地放置。</li>
<li>Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠</li>
<li>每个元素的margin box的左边， 与包含块border box的左边相接触(对于从左往右的格式化，否则相反)。即使存在浮动也是如此。</li>
<li>BFC的区域不会与float box重叠。</li>
<li>BFC就是页面上的一个隔离的独立容器，容器里面的子元素不会影响到外面的元素。反之也如此。</li>
<li>计算BFC的高度时，浮动元素也参与计算</li>
</ol>
<blockquote>
<p>参考资料<br><a target="_blank" rel="noopener" href="https://www.w3.org/TR/2011/REC-CSS2-20110607/visuren.html#block-formatting">W3C-CSS2标准原版</a><br><a target="_blank" rel="noopener" href="http://www.ayqy.net/doc/css2-1/visuren.html#block-formatting">W3C-CSS2标准中文翻译版</a><br><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV16b411H7Pz">动画教程-如何快速理解BFC</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/page/tags/CSS/" rel="tag"># CSS</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/page/2020/08/26/%E5%90%84%E7%A7%8D%E5%AE%9A%E5%AE%BD%E8%87%AA%E9%80%82%E5%BA%94%E5%B8%83%E5%B1%80%E6%96%B9%E5%BC%8F/" rel="prev" title="各种定宽自适应布局方式">
                  <i class="fa fa-chevron-left"></i> 各种定宽自适应布局方式
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/page/2020/08/30/%E8%8A%82%E6%B5%81%E3%80%81%E9%98%B2%E6%8A%96%E5%87%BD%E6%95%B0/" rel="next" title="节流、防抖函数">
                  节流、防抖函数 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">爱爱爱呵风他</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/page/js/comments.js"></script><script src="/page/js/utils.js"></script><script src="/page/js/motion.js"></script><script src="/page/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/page/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"assd12138/utterancesComment","issue_term":"pathname","theme":"preferred-color-scheme"}</script>
<script src="/page/js/third-party/comments/utterances.js"></script>

</body>
</html>
